<?php //pack11class

class ObjectsListNavigation extends Module {

    const COUNT=10;
    const ADMIN_COUNT=30;
    public $back_arrow="";
    public $forward_arrow="";
    private static $current=1;
    private static $objectsCount=0;
    public $traceEnabled=true;
    private static $columns=array();
    private static $columnsDone=false;

    public function setup() {
        $count=self::getObjectsCount();
        $pg=self::getCount();
        if(!$count) return;
        if($count) {
            $pg_count=ceil($count/$pg);
            //trace(self::getPageNumber());
        }
        if($pg_count>1) {
            if(self::$current>1) $this->back_arrow=Template::html(new ObjectsListNavigationArrow(ObjectsListNavigationArrow::BACK));
            if(self::$current<$pg_count)$this->forward_arrow=Template::html(new ObjectsListNavigationArrow(ObjectsListNavigationArrow::FORWARD));
            for($i=1; $i<=$pg_count; $i++) {
                $this->addChild(new ObjectsListNavigationItem($i));
            }
        }
    }

    public static function getPageNumber() {
        $navigation=SettingsManager::getSetting("navigation");
        if(is_numeric($navigation->value)) self::$current=$navigation->value;
        if(self::$current<1) self::$current=1;
        //trace(self::$current);
        return self::$current;
    }
    
    public static function getObjectsCount() {
        if(self::$objectsCount>0) return self::$objectsCount;
        return false;
    }

    public static function setObjectsCount($n) {
        if(is_numeric($n)) self::$objectsCount=$n;
            else trace("Wrong type of objects count ".$n."!");
    }

    public static function getColumns() {
        if(self::$columnsDone) return self::$columns;
        array_push(Tracer::$traceFrom,__CLASS__);
        $columns=ObjectsManager::$sortable;
        $page=self::getPageNumber();
        if($page==0) {
            trace("Return sortable");
            $_SESSION["columns"]=NULL;
            self::$columns=$columns;
            return $columns;
        }
        if($page==1) {
            $colArray=array();
            foreach($columns as $column) array_push($colArray,$column->id);
            $_SESSION["columns"]=implode(",", $colArray);
            self::$columns=$columns;
            //trace("Set and return columns");
            return self::$columns;
        } else {
            if(array_key_exists("columns", $_SESSION)) {
                if(!$_SESSION["columns"]) {
                    //trace("!session -> Return sortable");
                    self::$columns=$columns;
                    return $columns;
                } else {
                    $currentColumns=array();
                    $colArray=explode(",",$_SESSION["columns"]);
                    foreach($colArray as $id) array_push($currentColumns,PropertiesManager::getProperty($id));
                    //trace("Return current columns");
                    self::$columns=$currentColumns;
                    return $currentColumns;
                }
            } else {
                //trace("not in session -> Return sortable");
                self::$columns=$columns;
                return $columns;
            }
        }
        return false;
    }

    public static function getCount() {
        $userin=SettingsManager::getSetting("userin");
        $vlue=($userin)? $userin->value:false;
        return ($vlue)? self::ADMIN_COUNT:self::COUNT;
    }

    public static function setColumns($s) {

    }

}
?>
